package com.yuyou.fn.timer.task;

import cn.beststudy.member.*;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.xxl.job.core.log.XxlJobLogger;
import com.yuyou.fn.common.service.ISiteConfigService;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Urls;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.service.IMemberStudentService;
import com.yuyou.fn.educational.service.IStudentService;
import com.yuyou.fn.educational.vo.StudentSaveOrUpdateVo;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 根据cid发生变动的数据处理
 */
@JobHander(value = "DealCidByMemberTask")
@Component
public class DealCidByMemberTask extends IJobHandler {

    @Resource
    private IStudentService studentService ;


    @Resource
    private ISiteConfigService siteConfigService ;

    private RegistrationService registrationService ;

    @Resource
    private IMemberStudentService memberStudentService;

    private static final Logger log = LoggerFactory.getLogger(DealCidByMemberTask.class);


    @Override
    public ReturnT<String> execute(String... params) throws Exception {

        registrationService =  new RegistrationService(Urls.create(siteConfigService.getConfigValueFromCache("member_webservice_url")));
        Date date=null;
        if (params!=null && params.length>0){
            date=Utils.parseDate(params[0],"yyyy-MM-dd HH:mm:ss");
        }else {
            date=TimeUtils.now();
            date=TimeUtils.changeSecond(date,-70);
        }

        try {
            List<MemberIDChangeDTO> memberIDChangeDTOList = memberStudentService.getSyncMemberIDChange(date);

            if (!Utils.isEmpty(memberIDChangeDTOList)){
                for (MemberIDChangeDTO memberIDChangeDTO:memberIDChangeDTOList){
                    Student oldStudent=studentService.findByCID(memberIDChangeDTO.getFromMemberID().toUpperCase());
                    Student newStudent=studentService.findByCID(memberIDChangeDTO.getToMemberID().toUpperCase());
                    if (oldStudent!=null && newStudent==null){
                        StudentSaveOrUpdateVo studentSaveOrUpdateVo=new StudentSaveOrUpdateVo();

                        studentSaveOrUpdateVo.setStudentId(oldStudent.getStudentId());
                        studentSaveOrUpdateVo.setCid(memberIDChangeDTO.getToMemberID().toUpperCase());
                        studentSaveOrUpdateVo.setName(memberIDChangeDTO.getToFullName());
                        studentSaveOrUpdateVo.setPhoneNo(memberIDChangeDTO.getToMainPhone());
                        studentSaveOrUpdateVo.setSlavePhoneNo(memberIDChangeDTO.getToSlavePhone());
                        try {
                            studentService.saveOrUpdateStudent(studentSaveOrUpdateVo);

                        }catch (Exception e){
                            log.error("fromCid:"+memberIDChangeDTO.getFromMemberID()+",toCid:"+memberIDChangeDTO.getToMemberID()+"失败,{}",e );
                        }
                    }

                }
            }

        } catch (Exception e) {
            XxlJobLogger.log("修改学生cid数据失败:{0}",e);
            return ReturnT.FAIL;
        }

        return ReturnT.SUCCESS;
    }

    private void bindToMemberCenter(String memberId, Long studentId) {
        Student student = studentService.findById(studentId , "cid");

        if(StringUtils.isNotEmpty(student.getCid())){
            try{
                registrationService.getRegistrationServiceSoap().changeMem2MemRelationIsValid(memberId,student.getCid(),true );
            }catch (Exception e){
                e.printStackTrace();
                //
            }
        }
    }

}
